package com.example.vv.model.projectItemDetails;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.vv.R;
import com.example.vv.fragmentWork.entity.ProjectListEntity;
import com.example.vv.fragmentWork.mvp.MvpActivity;
import com.example.vv.model.projectItemDetails.base.contract.PIDContract;
import com.example.vv.model.projectItemDetails.base.presenter.PIDPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PIDActivity extends MvpActivity<PIDContract.View, PIDPresenter> implements PIDContract.View {

    @BindView(R.id.title_toolbar)
    Toolbar titleToolbar;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.hid_web)
    WebView hidWeb;

    @Override
    public int initLayout() {
        return R.layout.activity_p_i_d;
    }

    @Override
    public PIDPresenter initPresenter() {
        return new PIDPresenter();
    }

    @SuppressLint("NewApi")
    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        titleToolbar.setNavigationOnClickListener(v->{
            finish();
        });
    }

    @Override
    public void initData() {

    }
    /**
     * 接收项目数据
     *
     * @param bean
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getData(ProjectListEntity.DatasBean bean) {
        titleContent.setText(bean.getAuthor());
        hidWeb.loadUrl(bean.getLink());
        initWeb();
    }

    public void initWeb() {
        hidWeb.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        hidWeb.setLayerType(View.LAYER_TYPE_HARDWARE,null);
        WebSettings settings = hidWeb.getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setUseWideViewPort(true);
        settings.setAllowFileAccess(true);
        settings.setSupportZoom(false);
        settings.setDisplayZoomControls(false);
        settings.setBuiltInZoomControls(false);
        settings.setJavaScriptEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    }

    @Override
    public void doBusiness() {

    }

    @Override
    public View titleView() {
        return null;
    }

    @Override
    public Context getMContext() {
        return getMContext();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}